package zack.project.domain.trade.model.valobj;

import zack.project.types.enums.GroupBuyOrderEnumVO;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Arrays;

/**
 * 退单类型
 *
 * @author A1793
 */

@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum RefundTypeVO {

    UNPAID_UNLOCK("unpaid_unlock", "unpaid_refund", "未支付,未成团") {
        @Override
        public boolean match(TradeOrderStatusEnumVO tradeOrderStatusEnumVO, GroupBuyOrderEnumVO groupBuyOrderEnumVO) {
            if (tradeOrderStatusEnumVO.getCode().equals(TradeOrderStatusEnumVO.CREATE.getCode())
                    && groupBuyOrderEnumVO.getCode().equals(GroupBuyOrderEnumVO.PROGRESS.getCode())) {
                return true;
            }
            return false;

        }
    },
    PAID_UNFORMED("paid_unformed", "paid_refund", "已支付,未成团") {
        @Override
        public boolean match(TradeOrderStatusEnumVO tradeOrderStatusEnumVO, GroupBuyOrderEnumVO groupBuyOrderEnumVO) {
            if (tradeOrderStatusEnumVO.getCode().equals(TradeOrderStatusEnumVO.COMPLETE) &&
                    groupBuyOrderEnumVO.getCode().equals(GroupBuyOrderEnumVO.PROGRESS.getCode())) {
                return true;
            }
            return false;
        }
    },
    PAID_FORMED("paid_formed", "paidTeam_refund", "已支付,已成团") {
        @Override
        public boolean match(TradeOrderStatusEnumVO tradeOrderStatusEnumVO, GroupBuyOrderEnumVO groupBuyOrderEnumVO) {
            if ((groupBuyOrderEnumVO.getCode().equals(GroupBuyOrderEnumVO.COMPLETE.getCode()) || groupBuyOrderEnumVO.getCode().equals(GroupBuyOrderEnumVO.COMPLETE_FAIL.getCode()))
                    && tradeOrderStatusEnumVO.getCode().equals(TradeOrderStatusEnumVO.COMPLETE.getCode())) {
                return true;
            }

            return false;
        }
    };


    String code;
    String strategy;
    String info;

    public abstract boolean match(TradeOrderStatusEnumVO tradeOrderStatusEnumVO, GroupBuyOrderEnumVO groupBuyOrderEnumVO);
    public static RefundTypeVO getRefundStrategy(TradeOrderStatusEnumVO tradeOrderStatusEnumVO, GroupBuyOrderEnumVO groupBuyOrderEnumVO) {
      return   Arrays.stream(values())
                .filter(vo->vo.match(tradeOrderStatusEnumVO, groupBuyOrderEnumVO)).findFirst()
                .orElseThrow(() -> new RuntimeException("不支持的退款状态组合: groupBuyOrderStatus=" + groupBuyOrderEnumVO + ", tradeOrderStatus=" + tradeOrderStatusEnumVO));

    }
    public static RefundTypeVO getRefundTypeEnumVOByCode(String code) {
        switch (code) {
            case "unpaid_unlock":
                return UNPAID_UNLOCK;
            case "paid_unformed":
                return PAID_UNFORMED;
            case "paid_formed":
                return PAID_FORMED;
        }
        throw new RuntimeException("退单类型枚举值不存在: " + code);
    }


}

